package com.example.entities;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

public class ContactEntity extends SQLiteOpenHelper {
  private static final String DATABASE_NAME="contactlist.db";
  private static final int SCHEMA_VERSION=1;
  
  public ContactEntity(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE contact (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, number TEXT, type TEXT, notes TEXT);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // no-op, since will not be called until 2nd schema
    // version exists
  }

  public Cursor getAll() {
    return(getReadableDatabase()
            .rawQuery("SELECT _id, name, number, type, notes FROM contact ORDER BY name",
                      null));
  }
  
  public void insert(String name, String number,
                     String type, String notes) {
    ContentValues cv=new ContentValues();
          
    cv.put("name", name);
    cv.put("number", number);
    cv.put("type", type);
    cv.put("notes", notes);
    
    getWritableDatabase().insert("contact", "name", cv);
  }
  
  public String getName(Cursor c) {
    return(c.getString(1));
  }
  
  public String getNumber(Cursor c) {
    return(c.getString(2));
  }
  
  public String getType(Cursor c) {
    return(c.getString(3));
  }
  
  public String getNotes(Cursor c) {
    return(c.getString(4));
  }
}
